
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapSizeToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllCategoryInProductSizeCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductCategoryToView(responseContent);
        }
    });

}

function bindEvent() {
    ISS.EVENT.bindEvent('#RegeditForm_size_Add1', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Thêm size mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#RegeditForm_size_Modified', 'click', function () {
        toEditPage();
    });

    ISS.EVENT.bindEvent('#RegeditForm_size_Delete', 'click', function () {
        deleteSize();
    });
}

function deleteSize() {
    var _maSize = [];
    ISS.DOM.forEach('#form-RegeditFormSize input[type="checkbox"]:checked', function(){
        var size = {};
        size.maSize = $(this).val()
        _maSize.push(size);
    });
    if(_maSize.length > 0) {
        ISS.BACKEND.request({
            url: '../../service/admin/header/registerProductSizeDelete',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(_maSize),
            success: function (data) {
                var responseContent = data.code;
                if(responseContent == 0) {
                    alert('Xoá thành công');
                    loadData();
                }
                else {
                    alert('Xoá không thành công');
                }
            }
        });
    }
    else {
        alert('Chưa chọn dòng nào');
    }

}

function sendInfo() {
    var size = {};

    size.maSize = ISS.DOM.getVal('#RegeditFormSize_code');
    size.tenSize = ISS.DOM.getVal('#RegeditFormSize_name');

    var category = {};
    category.maLoai = ISS.DOM.getVal('#RegeditFormSize_categorieslist_option');
    size.loaiDanhMuc = category;

    ISS.BACKEND.request({
        url: '../../service/admin/header/registerProductSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(size),
        success: function (data) {
            var responseContent = data.code;
            sendInfoComplete(responseContent);
        }
    });
}

function sendInfoComplete(data) {
    if(data == 0) {
        alert('Thêm thành công');
        // set time for wait
        ISS.WINDOW.location('/jsp/admin/ad-product-size-common.jsp');
    }
    else{
        alert('Thêm không thành công');
    }
}

function toEditPage() {
    var _maSize = [];
    ISS.DOM.forEach('#form-RegeditFormSize input[type="checkbox"]:checked', function(){
        _maSize.push($(this).val());
    });

    if(_maSize.length == 1) {
        var _url = '/service/admin/toProductSizePage/' + _maSize[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Không được chọn hơn một dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#RegeditFormSize_code', 'error');
    ISS.DOM.removeClass('#RegeditFormSize_name', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapSizeToView(size) {

    var rows = size.length;
    var columns = 1;
    var str = '';
    str += '<table id="SizeTable" class="tableSizeWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSizeSTT">STT</th>';
    str += '       <th class="columnSizeStick">Tick</th>';
    str += '       <th class="columnSizeCode">Mã kích thước</th>';
    str += '       <th class="columnSizeName">Tên kích thước</th>';
    str += '       <th class="columnSize_Category">Loại danh mục</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }else{
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSizeSTT tableSizePadding">' + i + '</td>';
            str += '<td class="columnSizeStick tableSizePadding"><input onclick="highlight_row(this)" id="' + size[i].maSize + '" type="checkbox" value="' + size[i].maSize + '"/></td>';
            str += '<td class="columnSizeCode tableSizePadding">' + size[i].maSize + '</td>';
            str += '<td class="columnSizeName tableSizePadding">' + size[i].tenSize + '</td>';
            str += '<td class="columnSize_Category tableSizePadding">' + size[i].loaiDanhMuc.tenLoai + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#RegeditForm_size__All', str);
}

function mapAllProductCategoryToView(productcategory) {
    var html = '<option value="default_value" selected="selected">...</option>';
    for(var i=0; i<productcategory.length; i++) {

        html += '<option value="' + productcategory[i].maLoai + '">' + productcategory[i].tenLoai + '</option>';
    }
    ISS.DOM.html('#RegeditFormSize_categorieslist_option', html);
}